package com.passer.web.travel.controller;

import java.util.List;

import com.passer.web.travel.service.ITImagesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passer.common.annotation.Log;
import com.passer.common.enums.BusinessType;
import com.passer.web.travel.domain.TMeishi;
import com.passer.web.travel.service.ITMeishiService;
import com.passer.common.core.controller.BaseController;
import com.passer.common.core.domain.AjaxResult;
import com.passer.common.utils.poi.ExcelUtil;
import com.passer.common.core.page.TableDataInfo;

/**
 * 特色美食Controller
 *
 * @author passer
 * @date 2021-11-23
 */
@Controller
@RequestMapping("/travel/meishi")
public class TMeishiController extends BaseController
{
    private String prefix = "travel/meishi";

    @Autowired
    private ITMeishiService tMeishiService;


    @Autowired
    private ITImagesService tImagesService;

    @RequiresPermissions("travel:meishi:view")
    @GetMapping()
    public String meishi()
    {
        return prefix + "/meishi";
    }

    /**
     * 查询特色美食列表
     */
    @RequiresPermissions("travel:meishi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TMeishi tMeishi)
    {
        startPage();
        List<TMeishi> list = tMeishiService.selectTMeishiList(tMeishi);
        return getDataTable(list);
    }

    /**
     * 导出特色美食列表
     */
    @RequiresPermissions("travel:meishi:export")
    @Log(title = "特色美食", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TMeishi tMeishi)
    {
        List<TMeishi> list = tMeishiService.selectTMeishiList(tMeishi);
        ExcelUtil<TMeishi> util = new ExcelUtil<TMeishi>(TMeishi.class);
        return util.exportExcel(list, "特色美食数据");
    }

    /**
     * 新增特色美食
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存特色美食
     */
    @RequiresPermissions("travel:meishi:add")
    @Log(title = "特色美食", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TMeishi tMeishi)
    {
        int i = tMeishiService.insertTMeishi(tMeishi);
        //插入封面图片到图片库中
        tImagesService.saveCover(tMeishi);
        return toAjax(i);
    }

    /**
     * 修改特色美食
     */
    @GetMapping("/edit/{mid}")
    public String edit(@PathVariable("mid") Long mid, ModelMap mmap)
    {
        TMeishi tMeishi = tMeishiService.selectTMeishiById(mid);
        tMeishi.setCoverURL(tImagesService.selectCoverByBusiness(tMeishi).getSrc());
        mmap.put("tMeishi", tMeishi);
        return prefix + "/edit";
    }

    /**
     * 修改保存特色美食
     */
    @RequiresPermissions("travel:meishi:edit")
    @Log(title = "特色美食", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TMeishi tMeishi)
    {

        int i = tMeishiService.updateTMeishi(tMeishi);
        //插入封面图片到图片库中
        tImagesService.saveCover(tMeishi);
        return toAjax(i);
    }

    /**
     * 删除特色美食
     */
    @RequiresPermissions("travel:meishi:remove")
    @Log(title = "特色美食", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tMeishiService.deleteTMeishiByIds(ids));
    }
}
